package com.zh.mediaservice.timer;

import com.zh.mediacommon.constant.ReserveConstant;
import com.zh.mediapojo.vo.NotPayVO;
import com.zh.mediaservice.mapper.DoctorMapper;
import com.zh.mediaservice.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.time.LocalDateTime;

@Component
@Slf4j
public class ReserveScheduling {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderMapper orderMapper;

    @Scheduled(cron = "0 0 3 * * ?")
//    @PostConstruct
    @Async
    public void reserveScheduling() {
        log.info("预约人数更新定时器执行^_^");
        for (Integer did : doctorMapper.getDoctorId()) {
            HashOperations hashOperations = redisTemplate.opsForHash();
            Integer amNum = (Integer) hashOperations.get(did.toString(), ReserveConstant.NOW_NUM_AM);
            Integer pmNum = (Integer) hashOperations.get(did.toString(), ReserveConstant.NOW_NUM_PM);
            if(amNum == null)
                amNum = 0;
            if(pmNum == null)
                pmNum = 0;
            doctorMapper.updateNum(did,amNum,pmNum);
            hashOperations.put(did.toString(), ReserveConstant.NOW_NUM_AM,0);
            hashOperations.put(did.toString(), ReserveConstant.NOW_NUM_PM,0);
            Object o = hashOperations.get(did.toString(), ReserveConstant.MAX_NUM_AM);
            if (o==null)
                hashOperations.put(did.toString(), ReserveConstant.MAX_NUM_AM, ReserveConstant.DEFAULT_MAX_NUM_AM);
            Object o1 = hashOperations.get(did.toString(), ReserveConstant.MAX_NUM_PM);
            if (o1==null)
                hashOperations.put(did.toString(), ReserveConstant.MAX_NUM_PM, ReserveConstant.DEFAULT_MAX_NUM_PM);
        }
        //TODO

    }

    @PostConstruct
    @Scheduled(cron = "0 1 * * * ?")
    @Async
    public void checkNotPayOrder(){
        LocalDateTime time=LocalDateTime.now().minusMinutes(10);
        log.info("检查未支付的订单{}",time);
        List<NotPayVO> payList= orderMapper.selectNotPayOrder(time);
        for (NotPayVO notPayVO : payList) {
            String key=notPayVO.getReserveTime().equals("上午")?ReserveConstant.NOW_NUM_AM:ReserveConstant.NOW_NUM_PM;
            Integer num = (Integer) redisTemplate.opsForHash().get(notPayVO.getDid().toString(), key);
            if(num!=null && num>0){
                num-=1;
            }
            redisTemplate.opsForHash().put(notPayVO.getDid().toString(),key,num);
            orderMapper.updateNotPayOrderStatus(notPayVO.getId());
        }
    }

}
